<template>
  <div class="left">
    <h2>叩丁狼电商运营平台</h2>
    <el-icon :size="20" color="#fff" @click="isCollapse = !isCollapse">
      <Menu />
    </el-icon>
  </div>
  <el-dropdown :hide-on-click="false" @command="handleCommand">
    <span class="el-dropdown-link">
      {{ userStore.username }}
      <el-icon class="el-icon--right">
        <arrow-down />
      </el-icon>
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="首页">首页</el-dropdown-item>
        <el-dropdown-item command="退出">退出</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup lang="ts">
import { Menu, ArrowDown } from "@element-plus/icons-vue";
import { inject } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/store/user";
import { logout } from "@/common/Logout/index";
import { logoutAPI } from "@/views/login/api/index";
const router = useRouter();
const userStore = useUserStore();
const isCollapse = inject("isCollapse");
// 下拉列表点击事件
const handleCommand = (command: string) => {
  if (command === "首页") {
    ElMessage("回到首页");
    router.push("/index");
  } else if (command === "退出") {
    //退出登录
    logout(
      () => {
        logoutAPI();
      },
      () => {
        ElMessage.success("退出成功");
      }
    );
  }
};
</script>

<style lang="less" scoped>
.left {
  display: flex;
  align-items: center;

  h2 {
    margin-right: 20px;
    color: #fff;
  }

  :deep(.el-icon) {
    cursor: pointer;
  }

  // 下拉列表的样式
  :deep(.example-showcase .el-dropdown-link) {
    cursor: pointer;
    color: var(--el-color-primary);
    display: flex;
    align-items: center;
  }

  :depp(.el-dropdown) {
    cursor: pointer;
    display: flex;
    align-items: center;
    text-align: center;
  }
}
</style>
